package com.fuluwa.interfaces.category.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuluwa.domain.category.service.CategoryService;
import com.fuluwa.domain.category.service.dto.NavDTO;
import com.fuluwa.infra.utils.JSONUtil;
import com.fuluwa.infra.web.AbstractDomainController;
import com.fuluwa.infra.web.Result;
import com.fuluwa.interfaces.category.web.filter.CategoryFilter.CategoryValueFilter;
import com.fuluwa.interfaces.category.web.filter.NAVFilter.NAVPropFileter;

/**
 * 类目服务相关
 * 
 * @author leasenli
 * 
 */
@Controller
@RequestMapping(value = "/category")
public class CategoryController extends AbstractDomainController {

	@Autowired
	private CategoryService categoryService;

	@RequestMapping(value = "nav", method = RequestMethod.GET)
	@ResponseBody
	public Result nav() {
		try {
			NavDTO nav = categoryService.getNavRecurs(0);
			Result result = getSuccessResult();
			result.addData("nav", JSONUtil.object2Json(nav, new NAVPropFileter(), new CategoryValueFilter()));
			return result;
		} catch (Exception e) {
			return getFailResult("Get category nav fail");
		}
	}
	
	@RequestMapping(value = "current", method = RequestMethod.GET)
	@ResponseBody
	public Result current(@RequestParam long cid) {
		try {
			NavDTO nav = categoryService.getNav(cid);
			Result result = getSuccessResult();
			result.addData("current", JSONUtil.object2Json(nav, new NAVPropFileter(), new CategoryValueFilter()));
			return result;
		} catch (Exception e) {
			return getFailResult("Get category current fail");
		}
	}
}
